package cn.wjh.business.service.impl;

import java.util.List;

import cn.wjh.common.exception.ServiceException;
import cn.wjh.common.utils.DateUtils;
import cn.wjh.common.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import cn.wjh.business.mapper.LanguageGroupMapper;
import cn.wjh.business.domain.LanguageGroup;
import cn.wjh.business.service.ILanguageGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 语言编组Service业务层处理
 *
 * @author jiraWu
 * @date 2023-12-27
 */
@Service
public class LanguageGroupServiceImpl extends ServiceImpl<LanguageGroupMapper, LanguageGroup> implements ILanguageGroupService {
    @Resource
    private LanguageGroupMapper languageGroupMapper;

    /**
     * 查询语言编组
     *
     * @param languageGroupId 语言编组主键
     * @return 语言编组
     */
    @Override
    public LanguageGroup selectLanguageGroupByLanguageGroupId(Long languageGroupId) {
        return languageGroupMapper.selectLanguageGroupByLanguageGroupId(languageGroupId);
    }

    /**
     * 查询语言编组列表
     *
     * @param languageGroup 语言编组
     * @return 语言编组
     */
    @Override
    public List<LanguageGroup> selectLanguageGroupList(LanguageGroup languageGroup) {
        return languageGroupMapper.selectLanguageGroupList(languageGroup);
    }

    /**
     * 新增语言编组
     *
     * @param languageGroup 语言编组
     * @return 结果
     */
    @Override
    public int insertLanguageGroup(LanguageGroup languageGroup) {
        uniqueLanguageGroup(languageGroup);
        languageGroup.setCreateTime(DateUtils.getNowDate());
        return languageGroupMapper.insertLanguageGroup(languageGroup);
    }

    /**
     * 是否唯一
     */
    private void uniqueLanguageGroup(LanguageGroup languageGroup) {
        LanguageGroup one = this.getOne(new QueryWrapper<LanguageGroup>().lambda()
                .eq(LanguageGroup::getUserId, SecurityUtils.getUserId())
                .eq(LanguageGroup::getLanguageCodes, languageGroup.getLanguageCodes())
                .eq(languageGroup.getLanguageGroupId() != null, LanguageGroup::getLanguageGroupId, languageGroup.getLanguageGroupId())
                .last("limit 1"));
        if (one != null) {
            throw new ServiceException("该语言组已经存在!");
        }
    }

    /**
     * 修改语言编组
     *
     * @param languageGroup 语言编组
     * @return 结果
     */
    @Override
    public int updateLanguageGroup(LanguageGroup languageGroup) {
        uniqueLanguageGroup(languageGroup);
        return languageGroupMapper.updateLanguageGroup(languageGroup);
    }

    /**
     * 批量删除语言编组
     *
     * @param languageGroupIds 需要删除的语言编组主键
     * @return 结果
     */
    @Override
    public int deleteLanguageGroupByLanguageGroupIds(Long[] languageGroupIds) {
        return languageGroupMapper.deleteLanguageGroupByLanguageGroupIds(languageGroupIds);
    }

    /**
     * 删除语言编组信息
     *
     * @param languageGroupId 语言编组主键
     * @return 结果
     */
    @Override
    public int deleteLanguageGroupByLanguageGroupId(Long languageGroupId) {
        return languageGroupMapper.deleteLanguageGroupByLanguageGroupId(languageGroupId);
    }
}
